package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.formation.Formation;
import com.xcity.game.peakroad.PeakRoadUtils;
import com.xcity.game.peakroad.model.PeakRoadInstance;
import com.xcity.game.peakroad.model.response.Floor;
import com.xcity.game.peakroad.template.PeakRoadTemplate;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class PeakRoadResponse extends Response {

	public long[] formation; // 员工战斗阵型
	public int topFloor; // 最高层数
	public int currentFloor; // 当前所在层数
	@Deprecated
	public int preFloor; // 上次挑战到的最高层数
	public byte sweepable; // 是否可扫荡
	public int leftTimes; // 剩余挑战次数
	public int maxTimes; // 总挑战次数
	public List<Floor> list = Collections.emptyList(); // 楼层信息
	
	public PeakRoadResponse(int serial, Player player, int offset, int count) {
		super(serial);
		PeakRoadInstance instance = player.getPeakRoadInstance();
		Employee[] units = player.getFormations().get(Formation.TYPE_PEAK_ROAD).getUnits();
		this.formation = new long[units.length];
		for (int i = 0; i < units.length; i++) {
			Employee unit = units[i];
			if (unit != null && !instance.isDied(unit)) {
				this.formation[i] = unit.getId();
			}
		}
		this.topFloor = PeakRoadUtils.getTopFloor();
		this.currentFloor = instance.getFloor();
		this.preFloor = instance.getLastMaxFloor();
		this.sweepable = (byte) (PeakRoadUtils.isSweepable(player) ? 1 : 0);
		this.leftTimes = PeakRoadUtils.getLeftTimes(player);
		this.maxTimes = PeakRoadUtils.getMaxTimes();
		if (offset < 1 || offset > this.topFloor) {
			offset = instance.getFloor();
		}
		if (count < 1) {
			count = 1;
		} else if (count > 10) { // max can request 10
			count = 10;
		}
		List<PeakRoadTemplate> refers = PeakRoadUtils.getTemplates();
		int size = --offset + count;
		if (size > refers.size()) {
			size = refers.size();
		}
		int cap = size - offset;
		this.list = new ArrayList<Floor>(cap);
		for (int i = offset; i < size; i++) {
			PeakRoadTemplate refer = refers.get(i);
			this.list.add(new Floor(player, refer));
		}
	}

}
